<template>
	<view closeable :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="value" length="auto"
		:safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex" :border-radius="borderRadius"
		:closeable="closeable">
		<view class="u-calendar">
			<!-- 			<view class="u-calendar__header">
				<view class="u-calendar__header__text" v-if="!$slots['tooltip']">
					{{toolTip}}
				</view>
				<slot v-else name="tooltip" />
			</view> -->
			<view class="u-calendar__action u-flex u-row-center">
				<!-- <view class="u-calendar__action__icon">
					<u-icon v-if="changeYear" name="arrow-left-double" :color="yearArrowColor" @click="changeYearHandler(0)"></u-icon>
				</view> -->
				<!-- 切换月分 -->
				<view style="width: 220rpx;padding-left: 150rpx;" @click="changeMonthHandler(0)" class="u-calendar__action__icon">
					<u-icon size="42" v-if="changeMonth" name="arrow-left" :color="monthArrowColor"
						></u-icon>
				</view>
				
				<view class="u-calendar__action__text">{{ showTitle }}</view>
				
				<view style="width: 220rpx;" @click="changeMonthHandler(1)"  class="u-calendar__action__icon">
					<u-icon size="40" v-if="changeMonth" name="arrow-right" :color="monthArrowColor"
						></u-icon>
				</view>
				
				<!-- <view class="u-calendar__action__icon">
					<u-icon v-if="changeYear" name="arrow-right-double" :color="yearArrowColor" @click="changeYearHandler(1)"></u-icon>
				</view> -->
			</view>
			<view class="u-calendar__week-day">
				<view class="u-calendar__week-day__text" v-for="(item, index) in weekDayZh" :key="index">{{ item }}
				</view>
			</view>
			<view class="u-calendar__content">
				<!-- 前置空白部分 -->
				<block v-for="(item, index) in weekdayArr" :key="index">
					<view class="u-calendar__content__item"></view>
				</block>

				<view style="border:5px solid transparent" class="u-calendar__content__item" :class="{
						addclass: index === number,
						ycqq: yycqch(index),
						'u-hover-class': openDisAbled(year, month, index + 1),
						'u-calendar__content--start-date': (mode == 'range' && startDate == `${year}-${month}-${index + 1}`) || mode == 'date',
						'u-calendar__content--end-date': (mode == 'range' && endDate == `${year}-${month}-${index + 1}`) || mode == 'date'
					}" :style="{ backgroundColor: getColor(index, 1) }" v-for="(item, index) in daysArr" :key="index"
					@tap="dateClick(index)">
					<view class="u-calendar__content__item__inner" :style="{ color: getColor(index, 2) }">
						<view>{{ index + 1 }}</view>
						<!-- <view :class="{ plq: plq == `${year}-${month}-${index + 1}` }"></view> -->
						<view :class="{ yyq: payyq(index) }"></view>
						<view :class="{ plq: plq == datach(index) }"></view>
					</view>

					<!-- 开始，结束文字 -->
					<!-- <view class="u-calendar__content__item__tips" :style="{color:activeColor}" v-if="mode== 'range' && startDate==`${year}-${month}-${index+1}` && startDate!=endDate">{{startText}}</view> -->
					<!-- <view class="u-calendar__content__item__tips" :style="{color:activeColor}" v-if="mode== 'range' && endDate==`${year}-${month}-${index+1}`">{{endText}}</view> -->
				</view>
				<!-- <view class="u-calendar__content__bg-month">{{ month }}</view> -->

				<!-- 确定文字，选中时间 -->
				<!-- <view class="u-calendar__bottom">
					<view class="u-calendar__bottom__choose">
						<text>{{ mode == 'date' ? activeDate : startDate }}</text>
						<text v-if="endDate">至{{ endDate }}</text>
					</view>
					<view class="u-calendar__bottom__btn">
						<u-button :type="btnType" shape="circle" size="default" @click="btnFix()">确定</u-button>
					</view>
				</view> -->
				<u-modal v-model="show" title="提醒">
					<view class="slot-content">
						<rich-text :nodes="content"></rich-text>
					</view>
				</u-modal>
				<u-row style="width: 100%;">
					<u-col span="2" offset="0.5">
						<view class="row center">
							<view class="yjq"></view>
							<view class="demo-layout bg-purple">月经期</view>
						</view>
					</u-col>
					<u-col span="2">
						<view class="row center">
							<view class="yyq"></view>
							<view class="demo-layout bg-purple-light">易孕期</view>
						</view>
					</u-col>
					<u-col span="2">
						<view class="row center">
							<view class="pjr"></view>
							<view class="demo-layout bg-purple-dark">排卵日</view>
						</view>
					</u-col>
					<u-col span="3">
						<view class="row center">
							<view class="ycjq"></view>
							<view class="demo-layout bg-purple-dark">预测经期</view>
						</view>
					</u-col>
				</u-row>
			</view>
		</view>
	</view>
</template>
<script>
	/**
	 * calendar 日历
	 * @description 此组件用于单个选择日期，范围选择日期等，日历被包裹在底部弹起的容器中。
	 * @tutorial http://uviewui.com/components/calendar.html
	 * @property {String} mode 选择日期的模式，date-为单个日期，range-为选择日期范围
	 * @property {Boolean} v-model 布尔值变量，用于控制日历的弹出与收起
	 * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
	 * @property {Boolean} change-year 是否显示顶部的切换年份方向的按钮(默认true)
	 * @property {Boolean} change-month 是否显示顶部的切换月份方向的按钮(默认true)
	 * @property {String Number} max-year 可切换的最大年份(默认2050)
	 * @property {String Number} min-year 可切换的最小年份(默认1950)
	 * @property {String Number} min-date 最小可选日期(默认1950-01-01)
	 * @property {String Number} max-date 最大可选日期(默认当前日期)
	 * @property {String Number} 弹窗顶部左右两边的圆角值，单位rpx(默认20)
	 * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭日历(默认true)
	 * @property {String} month-arrow-color 月份切换按钮箭头颜色(默认#606266)
	 * @property {String} year-arrow-color 年份切换按钮箭头颜色(默认#909399)
	 * @property {String} color 日期字体的默认颜色(默认#303133)
	 * @property {String} active-bg-color 起始/结束日期按钮的背景色(默认#2979ff)
	 * @property {String Number} z-index 弹出时的z-index值(默认10075)
	 * @property {String} active-color 起始/结束日期按钮的字体颜色(默认#ffffff)
	 * @property {String} range-bg-color 起始/结束日期之间的区域的背景颜色(默认rgba(41,121,255,0.13))
	 * @property {String} range-color 选择范围内字体颜色(默认#2979ff)
	 * @property {String} start-text 起始日期底部的提示文字(默认 '开始')
	 * @property {String} end-text 结束日期底部的提示文字(默认 '结束')
	 * @property {String} btn-type 底部确定按钮的主题(默认 'primary')
	 * @property {String} toolTip 顶部提示文字，如设置名为tooltip的slot，此参数将失效(默认 '选择日期')
	 * @property {Boolean} closeable 是否显示右上角的关闭图标(默认true)
	 * @example <u-calendar v-model="show" :mode="mode"></u-calendar>
	 */
	import api from '@/api/fetation.js';
	export default {
		name: 'u-calendar',
		props: {
			timeAll: '',
			pjybi: '',
			safeAreaInsetBottom: {
				type: Boolean,
				default: false
			},
			// 是否允许通过点击遮罩关闭Picker
			maskCloseAble: {
				type: Boolean,
				default: true
			},
			// 通过双向绑定控制组件的弹出与收起
			value: {
				type: Boolean,
				default: false
			},
			// 弹出的z-index值
			zIndex: {
				type: [String, Number],
				default: 0
			},
			// 是否允许切换年份
			changeYear: {
				type: Boolean,
				default: true
			},
			// 是否允许切换月份
			changeMonth: {
				type: Boolean,
				default: true
			},
			// date-单个日期选择，range-开始日期+结束日期选择
			mode: {
				type: String,
				default: 'date'
			},
			// 可切换的最大年份
			maxYear: {
				type: [Number, String],
				default: 2050
			},
			// 可切换的最小年份
			minYear: {
				type: [Number, String],
				default: 1950
			},
			// 最小可选日期(不在范围内日期禁用不可选)
			minDate: {
				type: [Number, String],
				default: '1950-01-01'
			},
			/**
			 * 最大可选日期
			 * 默认最大值为今天，之后的日期不可选
			 * 2030-12-31
			 * */
			maxDate: {
				type: [Number, String],
				default: ''
			},
			// 弹窗顶部左右两边的圆角值
			borderRadius: {
				type: [String, Number],
				default: 20
			},
			// 月份切换按钮箭头颜色
			monthArrowColor: {
				type: String,
				default: '#606266'
			},
			// 年份切换按钮箭头颜色
			yearArrowColor: {
				type: String,
				default: '#909399'
			},
			// 默认日期字体颜色
			color: {
				type: String,
				default: '#303133'
				// default: 'red'
			},
			// 选中|起始结束日期背景色
			activeBgColor: {
				type: String,
				default: '#A460C4'
			},
			// 选中|起始结束日期字体颜色
			activeColor: {
				type: String,
				default: '#ffffff'
			},
			// 范围内日期背景色
			rangeBgColor: {
				type: String,
				// default: 'rgba(41,121,255,0.13)'
				default: 'RGBA(164, 96, 196, 0.5)'
			},
			// 范围内日期字体颜色
			rangeColor: {
				type: String,
				default: '#ffffff'
				// default: '#2979ff'
			},
			// mode=range时生效，起始日期自定义文案
			startText: {
				type: String,
				default: ''
			},
			// mode=range时生效，结束日期自定义文案
			endText: {
				type: String,
				default: ''
			},
			//按钮样式类型
			btnType: {
				type: String,
				default: 'primary'
			},
			// 当前选中日期带选中效果
			isActiveCurrent: {
				type: Boolean,
				default: true
			},
			// 切换年月是否触发事件 mode=date时生效
			isChange: {
				type: Boolean,
				default: false
			},
			// 是否显示右上角的关闭图标
			closeable: {
				type: Boolean,
				default: true
			},
			// 顶部的提示文字
			toolTip: {
				type: String,
				default: '选择日期'
			}
		},
		data() {
			return {
				show: false,
				content: `经期这么长是不是记错了？建议根据<br>实际情况修改你的月经时间和周期哦`,
				ycqq: '', //预测经期30-35
				yyq: '', //易孕期时间10-19
				daydi: '', //最后一次记录的第一天
				zztimeAll: '', //["2021-11-01", "2021-11-02", "2021-11-03", "2021-11-04", "2021-11-05"]
				plq: '', //排卵期时间14
				openid: '',
				number: '', //注意此处
				isactive: false,
				selectTime: '', //自己加的选中时间
				// 星期几,值为1-7
				weekday: 1, //当前年月第一天，是星期几0 1 2 3
				weekdayArr: [], //上个月空的天数[1,2,3,4,5]

				days: 0, // 当前月有多少天 30
				daysArr: [], //当前月天数的数组[1,2,3,4,5,6]
				showTitle: '',
				year: 2020,
				month: 0,
				day: 0,
				startYear: 0,
				startMonth: 0,
				startDay: 0,
				endYear: 0,
				endMonth: 0,
				endDay: 0,
				today: '',
				activeDate: '',
				startDate: '',
				endDate: '',
				isStart: true,
				min: null,
				max: null,
				weekDayZh: ['日', '一', '二', '三', '四', '五', '六']
			};
		},
		computed: {
			dataChange() {
				return `${this.mode}-${this.minDate}-${this.maxDate}`;
			},
			uZIndex() {
				// 如果用户有传递z-index值，优先使用
				return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
			}
		},
		watch: {
			dataChange(val) {
				this.init();
			},
			// startDate(val) {
			// 	console.log(1)
			// 	console.log(val)
			// 	console.log(2)
			// 	this.startDate = val
			// },


			timeAll: function(newData) {
				this.timeAll = newData;
				var num = this.timeAll.length;
				console.log('watch监听后台返回时间长度', num)
				if (num == 1) {
					this.daydi = this.timeAll[0]['time']
					console.log(this.daydi)
					console.log(this.timeAll[0])
				} else if (num > 0) {
					var arrln = [];
					this.timeAll.forEach(function(value) {
						arrln.push(value.time);
					});
					this.zztimeAll = arrln.reverse();
					console.log('监听后台传来日期', this.zztimeAll);
					
					var num = this.timeAll.length;

					var otime = this.timeAll[num - 1].time; //取到最后增加的一天
					
					console.log('监听后台传来日期的最后一天', otime);

					var arrlet = this.zztimeAll.reverse();
					
					console.log('666',arrlet)

					// var arr = [
					// 	'2016/02/28',
					// 	'2016/02/29',
					// 	'2017/02/25',
					// 	'2017/02/26',
					// 	'2017/02/27',
					// 	'2017/02/28'
					// ];
					var numf = this.lianxuDay(arrlet); //获取最后连续天数有几天

					console.log('监听后台传来日期的最后最后连续有几天', numf);
					
					var daydi = arrlet.reverse()[numf - 1];
					this.daydi = daydi;
					if(!daydi && otime){
						this.daydi = otime
					}
					
					

					console.log('监听后台传来日期的最后最后连续天数的第一天', daydi);

					this.plq = this.sal(daydi, 13);
					console.log('监听后台传来日期的最后最后连续天数的第一天+13天', this.plq);

					var ten = this.sal(daydi, 10);
					var sj = this.sal(daydi, 18);
					console.log('监听后台传来日期的最后最后连续天数的第一天+10天', ten);
					console.log('监听后台传来日期的最后最后连续天数的第一天+18天', sj);

					this.yyq = this.getAll(ten, sj);
					console.log('监听后台传来日期的最后最后连续天数的第一天+10天-18天', this.yyq);

					var san = this.sal(daydi, 30);
					var sanw = this.sal(daydi, 34);
					console.log('监听后台传来日期的最后最后连续天数的第一天+30天', san);
					console.log('监听后台传来日期的最后最后连续天数的第一天+34天', sanw);
					this.ycqq = this.getAll(san, sanw);
					console.log('监听后台传来日期的最后最后连续天数的第一天+30天-34天', this.ycqq);
				} else {
					this.daydi = ''
					this.zztimeAll = ''
					this.ycqq = ''
					this.yyq = ''
					this.plq = ''
				}
			}
		},
		created() {
			this.openid = uni.getStorageSync('openid');
			this.init();
		},
		methods: {
			//新加切换按钮  1相等  2大于  3小于
			async chenClick(status) {
				console.log('后台最后连续天数的第一天+切换按钮', this.daydi);
				console.log('点击时间+切换按钮', this.selectTime);

				var lxnun = this.comparedate(this.selectTime, this.daydi);
				console.log('选择时间跟后台返回连续时间的第一天比较+点击切换按钮时', lxnun)

				console.log('后台返回的连续时间数组+点击切换按钮时', this.zztimeAll)



				if (lxnun == 3 && this.zztimeAll && this.dayid) {
					this.selectTime = this.daydi
					uni.showModal({
						content: '以前的不能修改！',
						showCancel: false,
						success: () => {
							console.log('点击时间超出范围了')
							
							
							// this.navTo(`/pages/fetation/index`);
						}
					});
					return
				}

				var hdeb = this.sal(this.daydi, 28);
				var hb2bb = this.comparedate(this.selectTime, hdeb); //点击时间跟后台连续天数的第一天+28天比较

				var hdwt = this.sal(this.zztimeAll[0], 5); //后台返回时间最后天加5天
				var overnun = this.comparedate(this.selectTime, hdwt)
				console.log('选择时间跟后台返回最后一天+5天比较+点击切换按钮时', lxnun)

				if (hb2bb == 2 && overnun != 2) {
					this.show = true
					return
				}


				var statime = this.comparedate(this.selectTime, this.daydi);
				console.log('点击时间<后台最后连续天数的第一天==3+切换按钮+直接返回', statime);
				console.log('点击时间<后台最后连续天数的第一天', this.zztimeAll[0]);
				if (statime == 3 && this.zztimeAll[0]) {
					
					var data = {
						date: this.selectTime,
						dymzt: 0 //状态 2来了-关 1走了-开
					};
					this.$emit('funclti', data);
					
					return;
				} else {
					this.startDate = this.selectTime;
					var fhzhtime = this.zztimeAll[0]; //后台返回最后的一个时间
					var arrin = this.zztimeAll.includes(this.selectTime); // 选择时间是否在以前数组中



					console.log('后台返回时间数组+点击切换按钮', this.zztimeAll);
					console.log('后台返回时间的最后一个+点击切换按钮', fhzhtime);
					if (!fhzhtime || (status && !arrin)) {
						var swuth = this.sal(this.selectTime, 4); //点击时间后5天

						console.log('切换按钮开启+点击时间', this.selectTime);
						console.log('切换按钮开启+点击时间后5天', swuth);

						//初使数据为空时，直接加后五天 新加5个  A
						if (!fhzhtime) {
							console.log('这是初使用化数据，一天都还没有增加的');
							this.endDate = swuth;
							var data = {
								date: this.selectTime,
								dymzt: 1 //状态 2来了-关 1走了-开
							};
							this.$emit('funclti', data);
						} else {
							var b5 = this.comparedate(this.selectTime, fhzhtime); //后台最后间时比较

							var hdwuth = this.sal(fhzhtime, 4);
							var hb5 = this.comparedate(this.selectTime, hdwuth); //选择时间跟---后台最后时间+5天后--比较



							//点击时间--大于后台时间--小于后台+5天时间，  新加1，2个 B
							if (b5 == 2 && hb5 == 3) {
								this.startDate = fhzhtime;
								this.endDate = this.selectTime;
							}
							//点击时间--大于后台时问--大于后台+5，   新加5个 C
							if (b5 == 2 && hb5 == 2) {
								this.startDate = this.selectTime;
								this.endDate = swuth;
							}
						}

						console.log('切换按钮开启+点击时间+计算前开始时间', this.startDate);
						console.log('切换按钮开启+点击时间+计算前结束时间', this.endDate);
						var arrAllTime = this.get(this.startDate, this.endDate); //时间数组


						console.log('计算出来的时间集合', arrAllTime);

						this.arrAllTime = arrAllTime; //自己点的5天
						var arra = [];
						// console.log(arrAllTime)
						arrAllTime.forEach((item, index) => {
							var arrlem = {
								time: item,
								ym: 1 //1是开起 2关闭
							};
							arra.push(arrlem);
						});

					
						// console.log(arra)
						let dataArr = JSON.stringify(arra);
						await this.insertadd(dataArr); //后台插入数据
						var data = {
							date: this.selectTime,
							dymzt: 1 //状态 2来了-关 1走了-开
						};
						this.$emit('funclti', data);
						this.$emit('getlist'); //刷新数据
						this.$forceUpdate();
					} else if (!status) {
						console.log('走的关闭按钮');
						var lsarr = ''; //选中时间中否在后台时间数组中
						var cxwz = ''; //选中时间出现的位置
						var setime = this.selectTime;
						console.log('后台时间数组666',this.zztimeAll)
						// console.log(setime)
						this.zztimeAll.forEach(function(value, key) {
							if (value == setime) {
								lsarr = value;
								cxwz = key;
							}
						});
						console.log('选中时间-切换为关闭按钮', setime);
						console.log('后台时间数组-切换为关闭按钮', this.zztimeAll);
						console.log('选择时间是否在后组数中出现的位置-切换为关闭按钮', cxwz);
						//查到选中时间，在后台返回的数组中，开始时间要设置为空
						if (lsarr) {
							var letarrr = this.zztimeAll.slice(0, cxwz + 1);
							var arra = [];

							letarrr.forEach((item, index) => {
								var arrlem = {
									time: item,
									ym: 2 //1是开起 2关闭
								};
								arra.push(arrlem);
							});

							

							let dataArr = JSON.stringify(arra);
							await this.insertadd(dataArr);
							
							var data = {
								date: this.selectTime,
								dymzt: 2 //状态 2来了-关 1走了-开
							};
							this.$emit('funclti', data);
							this.startDate = '';
							this.endDate = '';
							this.$emit('getlist'); //刷新数据
							this.$forceUpdate();
						} else {
							this.startDate = ''; //关闭按钮没有在后台时间数组内的，开始时间要设置为空
							return;
						}
					}
				}
			},
			//点击一个日期
			dateClick: async function(day) {

				this.isactive = true;
				var dymzt = ''
				console.log('时间插件点击时间', day);
				day += 1;

				//选中的时间
				let datez = `${this.year}-${this.month}-${day}`;
				var date = this.formatDate(datez);
				
			
				
				await this.$emit('funclti', {date:date});
				
				console.log('选中时间+点击时间按钮时', date)

				let now = new Date();
				let nowt = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`; //当前年月日
				let nowtime = this.formatDate(nowt);

				console.log('当前今天时间+点击时间按钮时', nowtime)

				console.log('后台返回时间连续时间的第一天+点击时间按钮时', this.daydi)
				var lxnun = this.comparedate(date, this.daydi);
				console.log('选择时间跟后台返回连续时间的第一天比较+点击时间按钮时', lxnun)

				console.log('后台返回的连续时间数组+点击时间按钮时', this.zztimeAll)


				// if(lxnun==3 && this.zztimeAll){
				// 	this.selectTime = this.daydi
				// 	uni.showModal({
				// 		content: '以前的不能修改！',
				// 		showCancel: false,
				// 		success: () => {
				// 			console.log('点击时间超出范围了')

				// 			// this.navTo(`/pages/fetation/index`);
				// 		}
				// 	});
				// 	return
				// }


				//比较两个日期的大小 1相等  2大于  3小于
				var bnun = this.comparedate(date, nowtime);
				
				console.log(nowtime)
				console.log('比较选中时间与当前时间两个日期的大小 1相等  2大于  3小于+点击时间按钮时', bnun)
				if (bnun == 2) {
					this.selectTime = nowtime
					uni.showModal({
						content: '时间超过当前时间！',
						showCancel: false,
						success: () => {
							console.log('点击时间大于当前时间了')

							// this.navTo(`/pages/fetation/index`);
						}
					});
					return
				}
				console.log('标记+点击时间按钮时---不不不')
				this.selectTime = date
				this.number = day - 1; //重要处
				this.day = day;

				if (!this.openDisAbled(this.year, this.month, day)) {

					var arrin = this.zztimeAll.includes(this.selectTime); // 选择时间是否在以前数组中
					console.log('选中时间是否在后台时间里+点击一个日期arrin', arrin)
					console.log('后台时间+点击一个日期', this.zztimeAll)
					console.log('选中时间+点击一个日期', this.selectTime)
					if (arrin) {
						var dymzt = 1
					} else {
						var dymzt = 2
					}

					var data = {
						date: date,
						dymzt: dymzt //状态 2来了-关 1走了-开
					};
					console.log('状态', data)
					this.$emit('funclti', data);
					this.selectTime = date; //自己加的选中时间
					if (this.mode == 'date') {
						this.activeDate = date;
					} else {
						//判断是开始时间还是结束时间
						// let compare = new Date(date.replace(/\-/g, '/')).getTime() < new Date(this.startDate.replace(/\-/g, '/')).getTime();
						// if (this.isStart || compare) {
						// this.startDate = date;
						// this.startYear = this.year;
						// this.startMonth = this.month;
						// this.startDay = this.day;
						// this.endYear = 0;
						// this.endMonth = 0;
						// this.endDay = 0;
						// this.endDate = '';
						// this.activeDate = '';
						// this.isStart = false;
						// } else {
						// this.endDate = date;
						// this.endYear = this.year;
						// this.endMonth = this.month;
						// this.endDay = this.day;
						// this.isStart = true;
						// }
					}
				}
			},

			//根据开始日期和结束日期获取所有日期的方法

			get(day1, day2) {
				var getDate = function(str) {
					var tempDate = new Date();
					var list = str.split('-');
					tempDate.setFullYear(list[0]);
					tempDate.setMonth(list[1] - 1);
					tempDate.setDate(list[2]);
					return tempDate;
				};
				var date1 = getDate(day1);
				var date2 = getDate(day2);
				if (date1 > date2) {
					var tempDate = date1;
					date1 = date2;
					date2 = tempDate;
				}
				date1.setDate(date1.getDate() + 1);
				var dateArr = [];
				var i = 0;
				while (!(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1
						.getDate() == date2.getDate())) {
					var dayStr = date1.getDate().toString();
					if (dayStr.length == 1) {
						dayStr = '0' + dayStr;
					}
					var monthStr = (date1.getMonth() + 1).toString();
					if (monthStr.length == 1) {
						monthStr = '0' + monthStr;
					}
					dateArr[i] = date1.getFullYear() + '-' + monthStr + '-' + dayStr;
					i++;

					date1.setDate(date1.getDate() + 1);
				}
				dateArr.splice(0, 0, day1);
				dateArr.push(day2);

				return dateArr;
			},

			// 计算续住的总日期列表
			getAll(begin, end) {
				let arr1 = begin.split('-');
				let arr2 = end.split('-');
				let arr1_ = new Date();
				let arrTime = [];
				arr1_.setUTCFullYear(arr1[0], arr1[1] - 1, arr1[2]);
				let arr2_ = new Date();
				arr2_.setUTCFullYear(arr2[0], arr2[1] - 1, arr2[2]);
				let unixDb = arr1_.getTime();
				let unixDe = arr2_.getTime();
				for (let k = unixDb; k <= unixDe;) {
					arrTime.push(this.datetimeparse(k, 'YY-MM-DD'));
					k = k + 24 * 60 * 60 * 1000;
				}
				return arrTime;
			},
			// 时间格式处理
			datetimeparse(timestamp, format, prefix) {
				if (typeof timestamp == 'string') {
					timestamp = Number(timestamp);
				}
				//转换时区
				let currentZoneTime = new Date(timestamp);
				let currentTimestamp = currentZoneTime.getTime();
				let offsetZone = currentZoneTime.getTimezoneOffset() / 60; //如果offsetZone>0是西区，西区晚
				let offset = null;
				//客户端时间与服务器时间保持一致，固定北京时间东八区。
				offset = offsetZone + 8;
				currentTimestamp = currentTimestamp + offset * 3600 * 1000;

				let newtimestamp = null;
				if (currentTimestamp) {
					if (currentTimestamp.toString().length === 13) {
						newtimestamp = currentTimestamp.toString();
					} else if (currentTimestamp.toString().length === 10) {
						newtimestamp = currentTimestamp + '000';
					} else {
						newtimestamp = null;
					}
				} else {
					newtimestamp = null;
				}
				let dateobj = newtimestamp ? new Date(parseInt(newtimestamp)) : new Date();
				let YYYY = dateobj.getFullYear();
				let MM = dateobj.getMonth() > 8 ? dateobj.getMonth() + 1 : '0' + (dateobj.getMonth() + 1);
				let DD = dateobj.getDate() > 9 ? dateobj.getDate() : '0' + dateobj.getDate();
				let HH = dateobj.getHours() > 9 ? dateobj.getHours() : '0' + dateobj.getHours();
				let mm = dateobj.getMinutes() > 9 ? dateobj.getMinutes() : '0' + dateobj.getMinutes();
				let ss = dateobj.getSeconds() > 9 ? dateobj.getSeconds() : '0' + dateobj.getSeconds();
				let output = '';
				let separator = '/';
				if (format) {
					separator = format.match(/-/) ? '-' : '/';
					output += format.match(/yy/i) ? YYYY : '';
					output += format.match(/MM/) ? (output.length ? separator : '') + MM : '';
					output += format.match(/dd/i) ? (output.length ? separator : '') + DD : '';
					output += format.match(/hh/i) ? (output.length ? ' ' : '') + HH : '';
					output += format.match(/mm/) ? (output.length ? ':' : '') + mm : '';
					output += format.match(/ss/i) ? (output.length ? ':' : '') + ss : '';
				} else {
					output += YYYY + separator + MM + separator + DD;
				}
				output = prefix ? prefix + output : output;

				return newtimestamp ? output : '';
			},

			//查找几天后的时间
			sal(daydi, tnum) {
				var current = new Date(daydi);
				var newDate = this.dateAdd('d ', tnum, current);

				var day = newDate.getDate();
				var numb = day.toString().length;

				var dayh = `${newDate.getFullYear()}-${newDate.getMonth() + 1}-${day}`;

				var gsday = this.formatDate(dayh);

				return gsday;
			},
			lianxuDay(arr) {
				function time(date) {
					return new Date(date);
				}
				var num = 0; //声明计数变量1;
				var le = arr.length; //数组长度;
				for (var i = le; i > 0; i--) {
					if (time(arr[i - 1]) - time(arr[i - 2]) == 86400000) {
						num++;
					} else {
						break; //如果只要找出所有连续的天数,不需要中断
					}
				}
				if (num != 0) {
					return num + 1;
				}
			},
			checkeddymf(status) {
				console.log(status);
			},
			checkedtmf(status) {
				console.log(status);
			},
			//日期个位加0
			datach(index) {
				let day = index + 1;

				var bjdata = `${this.year}-${this.month}-${day}`;
				var gshdata = this.formatDate(bjdata);

				return gshdata;
			},
			//判断易孕期
			payyq(index) {
				var nowdata = this.datach(index);
				var sate = this.yyq.indexOf(nowdata); // -1
				if (sate != -1) {
					return true;
				} else {
					return false;
				}
			},

			//判断易孕期
			yycqch(index) {
				var nowdata = this.datach(index);
				var sate = this.ycqq.indexOf(nowdata); // -1
				if (sate != -1) {
					return true;
				} else {
					return false;
				}
			},
			getColor(index, type) {
				let color = type == 1 ? '' : this.color;
				let day = index + 1;

				var bjdata = `${this.year}-${this.month}-${day}`;
				var gshdata = this.formatDate(bjdata); //格式化日期

				//循环时间
				let date = `${this.year}-${this.month}-${day}`;

				let timestamp = new Date(date.replace(/\-/g, '/')).getTime();

				let start = this.startDate.replace(/\-/g, '/');
				// console.log(this.startDate)
				// console.log(start)
				let end = this.endDate.replace(/\-/g, '/');
				//循环时间与开始时间跟结束时间对比 判断显示颜色
				// if ((this.isActiveCurrent && this.activeDate == date) || this.startDate == date || this.endDate == date) {
				if (this.timeAll) {
					this.timeAll.forEach(function(value, key) {
						if (value.time == gshdata) {
							color = type == 1 ? '#A460C4' : '#ffffff';
						}
					});
				}

				if (this.startDate == date || this.endDate == date) {
					color = type == 1 ? this.activeBgColor : this.activeColor;
				} else if (this.endDate && timestamp > new Date(start).getTime() && timestamp < new Date(end).getTime()) {
					// color = type == 1 ? this.rangeBgColor : this.rangeColor;
				}
				return color;
			},
			init() {
				let now = new Date();
				this.year = now.getFullYear(); //当前年
				this.month = now.getMonth() + 1; //当前月

				this.day = now.getDate(); //当前日

				//默认选中时间
				this.number = this.day - 1;
				this.today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`; //当前年月日


				this.today = this.formatDate(this.today);
				this.selectTime = this.today; //自已加的
				this.activeDate = this.today;
				this.min = this.initDate(this.minDate); //最小可选日期

				this.max = this.initDate(this.maxDate || this.today); //最大可选日期
				this.startDate = '';
				this.startYear = 0;
				this.startMonth = 0;
				this.startDay = 0;
				this.endYear = 0;
				this.endMonth = 0;
				this.endDay = 0;
				this.endDate = '';
				this.isStart = true;
				this.changeData();
			},
			//切换111年月数据drf333
			changeData() {
				this.days = this.getMonthDay(this.year, this.month); // 当前月有多少天
				this.daysArr = this.generateArray(1, this.days); //当前月天数的数组[1,2,3,4,5,6]

				this.weekday = this.getWeekday(this.year, this.month); //当前年月第一天，是星期几0 1 2 3

				this.weekdayArr = this.generateArray(1, this.weekday); //上个月空的天数[1,2,3,4,5]

				this.showTitle = `${this.year}年${this.month}月`; //当前年月

				if (this.isChange && this.mode == 'date') {
					this.btnFix(true);
				}
			},

			async insertadd(dataArr) {
				var openid = uni.getStorageSync('openid');
				let tem = {
					openid: openid,
					data: dataArr
				};
				console.log(dataArr);
				const {
					code
				} = await api.submitRedayRecord(tem);

				if (code == 1) {
					return
					// uni.showToast({
					// 	title: '增加成功',
					// 	duration: 2000
					// });
				} else {
					uni.showToast({
						title: '增加失败',
						duration: 2000
					});
				}
			},

			//获取两日期之间日期列表函数
			getdiffdate(stime, etime) {
				if (stime && etime) {
					//初始化日期列表，数组
					var diffdate = new Array();
					var i = 0;

					//获取开始日期时间戳
					var stime_ts = new Date(stime).getTime();
					//获取结束日期时间戳
					var etime_ts = new Date(etime).getTime();

					//开始日期小于等于结束日期,并循环
					while (stime_ts <= etime_ts) {
						diffdate[i] = stime;
						console.log('当前日期：' + stime + '当前时间戳：' + stime_ts);
						//增加一天时间戳后的日期
						var next_date = stime_ts + 24 * 60 * 60 * 1000;
						//拼接年月日，这里的月份会返回（0-11），所以要+1
						var next_dates_y = new Date(next_date).getFullYear() + '-';
						var next_dates_m = new Date(next_date).getMonth() + 1 < 10 ? '0' + (new Date(next_date)
							.getMonth() + 1) + '-' : new Date(next_date).getMonth() + 1 + '-';
						var next_dates_d = new Date(next_date).getDate() < 10 ? '0' + new Date(next_date).getDate() :
							new Date(next_date).getDate();

						stime = next_dates_y + next_dates_m + next_dates_d;

						//增加数组key
						i++;
					}
					return diffdate;
				} else {
					return;
				}
			},

			//比较两个日期的大小 1相等  2大于  3小于
			comparedate(date1, date2) {
				var oDate1 = new Date(date1);
				var oDate2 = new Date(date2);
				if (oDate1.getTime() == oDate2.getTime()) {
					return 1;
				} else if (oDate1.getTime() > oDate2.getTime()) {
					return 2;
				} else {
					return 3;
				}
			},

			//格式化日期
			formatDate(date) {
				var d = new Date(date),
					month = '' + (d.getMonth() + 1),
					day = '' + d.getDate(),
					year = d.getFullYear();

				if (month.length < 2) month = '0' + month;
				if (day.length < 2) day = '0' + day;

				return [year, month, day].join('-');
			},
			//判断是不是在最小时间，最大时间内
			openDisAbled: function(year, month, day) {
				let bool = true;
				let date = `${year}/${month}/${day}`;
				// let today = this.today.replace(/\-/g, '/');
				let min = `${this.min.year}/${this.min.month}/${this.min.day}`;
				let max = `${this.max.year}/${this.max.month}/${this.max.day}`;

				let timestamp = new Date(date).getTime();

				if (timestamp >= new Date(min).getTime() && timestamp <= new Date(max).getTime()) {
					bool = false;
				}
				return bool;
			},
			//日期处理
			initDate(date) {
				let fdate = date.split('-');
				return {
					year: Number(fdate[0] || 1920),
					month: Number(fdate[1] || 1),
					day: Number(fdate[2] || 1)
				};
			},

			//一个日期几天后的日期
			dateAdd(interval, number, date) {
				switch (interval) {
					case 'y ': {
						date.setFullYear(date.getFullYear() + number);
						return date;
						break;
					}
					case 'q ': {
						date.setMonth(date.getMonth() + number * 3);
						return date;
						break;
					}
					case 'm ': {
						date.setMonth(date.getMonth() + number);
						return date;
						break;
					}
					case 'w ': {
						date.setDate(date.getDate() + number * 7);
						return date;
						break;
					}
					case 'd ': {
						date.setDate(date.getDate() + number);
						return date;
						break;
					}
					case 'h ': {
						date.setHours(date.getHours() + number);
						return date;
						break;
					}
					case 'm ': {
						date.setMinutes(date.getMinutes() + number);
						return date;
						break;
					}
					case 's ': {
						date.setSeconds(date.getSeconds() + number);
						return date;
						break;
					}
					default: {
						date.setDate(d.getDate() + number);
						return date;
						break;
					}
				}
			},
			generateArray: function(start, end) {
				return Array.from(new Array(end + 1).keys()).slice(start);
			},
			formatNum: function(num) {
				return num < 10 ? '0' + num : num + '';
			},
			//一个月有多少天
			getMonthDay(year, month) {
				let days = new Date(year, month, 0).getDate();
				return days;
			},
			getWeekday(year, month) {
				let date = new Date(`${year}/${month}/01 00:00:00`);
				return date.getDay();
			},
			//检查加减月是否在设肯范围内
			checkRange(year) {
				let overstep = false;
				if (year < this.minYear || year > this.maxYear) {
					uni.showToast({
						title: '日期超出范围啦~',
						icon: 'none'
					});
					overstep = true;
				}
				return overstep;
			},
			//加减月
			changeMonthHandler(isAdd) {
				// console.log(this.daysArr);
				this.isactive = false;
				this.number = '';
				if (isAdd) {
					let month = this.month + 1;
					let year = month > 12 ? this.year + 1 : this.year;
					if (!this.checkRange(year)) {
						this.month = month > 12 ? 1 : month;
						this.year = year;
						this.changeData();
					}
				} else {
					let month = this.month - 1;
					let year = month < 1 ? this.year - 1 : this.year;
					if (!this.checkRange(year)) {
						this.month = month < 1 ? 12 : month;
						this.year = year;
						this.changeData();
					}
				}
			},
			changeYearHandler(isAdd) {
				let year = isAdd ? this.year + 1 : this.year - 1;
				if (!this.checkRange(year)) {
					this.year = year;
					this.changeData();
				}
			},

			getWeekText(date) {
				date = new Date(`${date.replace(/\-/g, '/')} 00:00:00`);
				let week = date.getDay();
				return '星期' + ['日', '一', '二', '三', '四', '五', '六'][week];
			},
			btnFix() {
				if (!this.startDate || !this.endDate) return;
				let startMonth = this.formatNum(this.startMonth);
				let startDay = this.formatNum(this.startDay);
				let startDate = `${this.startYear}-${startMonth}-${startDay}`;
				let startWeek = this.getWeekText(startDate);

				let endMonth = this.formatNum(this.endMonth);
				let endDay = this.formatNum(this.endDay);
				let endDate = `${this.endYear}-${endMonth}-${endDay}`;
				let endWeek = this.getWeekText(endDate);
				this.$emit('change', {
					startYear: this.startYear,
					startMonth: this.startMonth,
					startDay: this.startDay,
					startDate: startDate,
					startWeek: startWeek,
					endYear: this.endYear,
					endMonth: this.endMonth,
					endDay: this.endDay,
					endDate: endDate,
					endWeek: endWeek
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	@import '../../libs/css/style.components.scss';

	.ycqq {
		// backgroundColor:#F9F3FE !important;
		background-color: rgba(235, 235, 235, 0.3) !important;
	}

	.plq {
		position: absolute;
		top: 70%;
		width: 14rpx;
		height: 14rpx;
		background: #fc682a;
		border-radius: 50%;
		left: 45%;
	}

	.yyq {
		position: absolute;
		top: 45%;
		width: 14rpx;
		height: 14rpx;
		background: #29c2b5;
		border-radius: 50%;
		left: 110%;
	}

	.addclass {
		// background-color: #520c9f;
		// border: 1rpx solid #520c9f;
		// border-radius: 5rpx;
		// border: 1px solid transparent;
		// :hover {
		// 	border: 1px solid red;
		// }
		border: 5rpx solid #520c9f !important;
		border-radius: 5rpx !important;
	}

	.u-calendar__content__item {
		padding: 2rpx 0 !important;
	}

	.u-calendar__content {
		padding: 10rpx 0 !important;
		background: #ffffff !important;
		box-shadow: 0rpx 4rpx 15rpx 1rpx rgba(233, 231, 234, 0.4) !important;
		border-radius: 0rpx 0rpx 40rpx 40px !important;
	}

	.u-calendar__week-day {
		padding: 0 0 !important;
	}

	.slot-content {
		text-align: center;
	}

	.yjq {
		width: 14rpx;
		height: 14rpx;
		background: #643a92;
		border-radius: 50%;
		margin-right: 10rpx;
	}

	.yyq {
		width: 14rpx;
		height: 14rpx;
		background: #29c2b5;
		border-radius: 50%;
		margin-right: 10rpx;
	}

	.pjr {
		width: 14rpx;
		height: 14rpx;
		background: #fc682a;
		border-radius: 50%;
		margin-right: 10rpx;
	}

	.ycjq {
		width: 14rpx;
		height: 14rpx;
		background: #d2d2d2;
		border-radius: 50%;
		margin-right: 10rpx;
	}

	.demo-layout {
		height: 72rpx;
		font-size: 24rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #999999;
		line-height: 72rpx;
	}

	.u-calendar {
		color: $u-content-color;

		&__header {
			width: 100%;
			box-sizing: border-box;
			font-size: 30rpx;
			background-color: #fff;
			color: $u-main-color;

			&__text {
				margin-top: 30rpx;
				padding: 0 60rpx;
				@include vue-flex;
				justify-content: center;
				align-items: center;
			}
		}

		&__action {
			padding: 40rpx 0 40rpx 0;

			&__icon {
				margin: 0 16rpx;
			}

			&__text {
				padding: 0 16rpx;
				color: $u-main-color;
				font-size: 32rpx;
				line-height: 32rpx;
				font-weight: bold;
			}
		}

		&__week-day {
			@include vue-flex;
			align-items: center;
			justify-content: center;
			padding: 6px 0;
			overflow: hidden;

			&__text {
				flex: 1;
				text-align: center;
			}
		}

		&__content {
			width: 100%;
			@include vue-flex;
			flex-wrap: wrap;
			padding: 6px 0;
			box-sizing: border-box;
			background-color: #fff;
			position: relative;

			&--end-date {
				border-top-right-radius: 8rpx;
				border-bottom-right-radius: 8rpx;
			}

			&--start-date {
				border-top-left-radius: 8rpx;
				border-bottom-left-radius: 8rpx;
			}

			&__item {
				width: 14.2857%;
				@include vue-flex;
				align-items: center;
				justify-content: center;
				padding: 6px 0;
				overflow: hidden;
				position: relative;
				z-index: 2;

				&__inner {
					height: 84rpx;
					@include vue-flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
					font-size: 32rpx;
					position: relative;
					border-radius: 50%;

					&__desc {
						width: 100%;
						font-size: 24rpx;
						line-height: 24rpx;
						transform: scale(0.75);
						transform-origin: center center;
						position: absolute;
						left: 0;
						text-align: center;
						bottom: 2rpx;
					}
				}

				&__tips {
					width: 100%;
					font-size: 24rpx;
					line-height: 24rpx;
					position: absolute;
					left: 0;
					transform: scale(0.8);
					transform-origin: center center;
					text-align: center;
					bottom: 8rpx;
					z-index: 2;
				}
			}

			&__bg-month {
				position: absolute;
				font-size: 130px;
				line-height: 130px;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				color: #e4e7ed;
				z-index: 1;
			}
		}

		&__bottom {
			width: 100%;
			@include vue-flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			background-color: #fff;
			padding: 0 40rpx 30rpx;
			box-sizing: border-box;
			font-size: 24rpx;
			color: $u-tips-color;

			&__choose {
				height: 50rpx;
			}

			&__btn {
				width: 100%;
			}
		}
	}
</style>
